package pl.wroc.pwr.mss.logic.domain;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.MappedSuperclass;

/**
 * Encja po ktorej dziedzicza wszystkie inne encje.
 * 
 * @author mjagus
 */
@MappedSuperclass
public class AbstractEntity implements Serializable
{
    /**
     * Identyfikator klasy.
     */
    private static final long serialVersionUID = -1479380418130066420L;

    /**
     * Systemowy identyfikator encji.
     */
    private Long id;

    /**
     * @return Systemowy identyfikator encji
     */
    @Id
    @Column(name = "ID", nullable = false)
    @GeneratedValue
    public Long getId()
    {
        return id;
    }

    /**
     * @param id Systemowy identyfikator do ustawienia
     */
    public void setId(Long id)
    {
        this.id = id;
    }

    @Override
    public boolean equals(Object obj)
    {
        if(this == obj)
            return true;

        if(obj == null)
            return false;

        try {
            final AbstractEntity other = (AbstractEntity) obj;
            if(this.getId() == null || other.getId() == null)
                return super.equals(other);

            if(this.getId() != other.getId() && (this.getId() == null || !this.getId().equals(other.getId())))
                return false;

            return true;
        }
        catch(ClassCastException e) {
            return false;
        }
    }

    @Override
    public int hashCode()
    {
        int hash = 3;
        if(this.getId() == null)
            return super.hashCode();

        hash = 17 * hash + (this.getId() != null ? this.getId().hashCode() : 0);
        return hash;
    }
}
